23 template <class T
> string
toStr(const T
&x
){
24 stringstream s
; s
<< x
; return s
.str();
27 template <class T
> int toInt(const T
&x
){
28 stringstream s
; s
<< x
; int r
; s
>> r
; return r
;
31 #define For(i, a, b) for (int i=(a); i<(b); ++i)
32 #define foreach(x, v) for (typeof (v).begin() x = (v).begin(); x != (v).end(); ++x)
33 #define D(x) cout << #x " = " << (x) << endl
35 const double EPS
= 1e-9;
36 int cmp(double x
, double y
, double tol
= EPS
){
37 return (x
<= y
+ tol
) ? (x
+ tol
< y
) ? -1 : 0 : 1;
41 vector
<int> d
[2 * MAXN
];
42 vector
<int> e
[2 * MAXN
];
44 #define minimize(x, y) if ((y) < (x)) (x) = (y)
46 bool startsWith(vector
<int> a
, vector
<int> b
){
47 assert(b
.size() <= a
.size());
48 for (int i
= 0; i
< b
.size(); ++i
){
49 if (a
[i
] != b
[i
]) return false;
54 void DV(vector
<int> v
){
55 for (int i
= 0; i
< v
.size(); ++i
){
61 int reflect(vector
<int> a
){
62 if (a
.size() == 1) return 0;
67 int half
= (a
.size() + 1) / 2;
69 for (int i
= half
; i
< a
.size(); ++i
){
71 for (int j
= 0; j
< half
; ++j
) l
.push_back(a
[i
]);
73 vector
<int> l_reversed
= l
;
74 reverse(l
.begin(), l
.end());
79 for (int k
= 0; k
< l_reversed
.size(); ++k
) t
.push_back(l_reversed
[k
]);
81 assert(t
.size() >= a
.size());
82 if (startsWith(t
, a
)){
83 minimize(ans
, t
.size() - a
.size());
87 for (int k
= 1; k
< l_reversed
.size(); ++k
) t
.push_back(l_reversed
[k
]);
88 assert(t
.size() >= a
.size());
89 if (startsWith(t
, a
)){
90 minimize(ans
, t
.size() - a
.size());
101 for (int i
= 0; i
< 2 * n
; ++i
){
106 for (int i
= 0; i
< n
; ++i
){
107 for (int j
= 0; j
<= i
; ++j
){
113 for (int i
= n
-2; i
>= 0; --i
){
114 for (int j
= 0; j
<= i
; ++j
){
117 d
[2*n
-2-i
].push_back(x
);
121 // for (int i = 0; i < 2 * n; ++i){
122 // for (int j = 0; j < d[i].size(); ++j){
123 // cout << d[i][j] << " ";
128 vector
<int> seen(2*n
, 0);
129 for (int j
= 0; j
< n
; ++j
){
131 for (int i
= (n
- 1) - j
; i
<= (n
- 1) + j
; i
+= 2){
132 e
[j
].push_back( d
[i
][seen
[i
]++] );
136 for (int j
= n
- 2; j
>= 0; --j
){
138 for (int i
= (n
- 1) - j
; i
<= (n
- 1) + j
; i
+= 2){
139 e
[2*n
-2 - j
].push_back( d
[i
][seen
[i
]++] );
144 // for (int i = 0; i < 2*n; ++i){
145 // for (int j = 0; j < e[i].size(); ++j){
146 // cout << e[i][j] << " ";
151 int max_a
= -1, max_b
= -1;
152 for (int i
= 0; i
< 2*n
; ++i
){
153 max_a
= max(max_a
, reflect(d
[i
]));
154 max_b
= max(max_b
, reflect(e
[i
]));
157 int add
= max_a
+ max_b
;
161 for (int i
= 0; i
< add
; ++i
){
171 for (int i
= 0; i
< T
; ++i
){
172 printf("Case #%d: ", i
);